const tf = require('@tensorflow/tfjs-node')
const fs = require('fs')
const test = async () => {
  // 读入模型
  // const label = await tf.node.getMetaGraphsFromSavedModel('file://1698656609368/model.json')
  // console.log(label)
  const mModel = await tf.loadLayersModel('file://1698663073380/model.json') // 2玫瑰，3雏菊
  // const mModel = await tf.node.loadSavedModel('file://1698656609368/model.json', ['serve'], 'serving_default') // 2玫瑰，3雏菊
  // console.log('mModel.classes_', mModel)
  // console.log('mModel.classes_', mModel.getClasses())
  // 把要预测的图片转换成张量
  const mTensor = tf.node.decodeImage(fs.readFileSync('../image/flowers/test/102501987_3cdb8e5394_n.jpg'))
  .resizeNearestNeighbor([96, 96])
  .toFloat()
  .div(tf.scalar(255.0))
  .expandDims()
  console.log(mTensor.dataSync())
  const labels = [1, 3]
  // 获取预测结果，主要靠predict这个方法进行预测
  const mResult = await mModel.predict(mTensor)
  // const mResult = await mModel.predict(mTensor)
  // const mResult = Array.from(mModel.predict(mTensor).dataSync()).map(num => num.toFixed(2))
    console.log(mResult)
  // const scores = await mResult['detection_scores'].arraySync();
  // const boxes = await mResult['detection_boxes'].arraySync();
  // const names = await mResult['detection_classes'].arraySync();
  // console.log(scores)
  // console.log(boxes)
  // console.log(names)
  mResult.print()
  console.log(mResult.dataSync())
  console.log('111', tf.argMax(mResult, 1))
  console.log('111', tf.argMax(mResult, 1).dataSync())
  mResult.array().then(array => console.log(array));
  // console.log('666', tf.argMax(mResult, axis=-1))
  // console.log(mResult.gather([0]).dataSync())
  // console.log(mResult.gather([1]).dataSync())
  // const predictions = mResult.data.map(value => value[0]);

}

test()